package com.bolt.report.business.modules.param.controller;

import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.report.business.modules.param.dto.ReportDbParamDTO;
import com.bolt.report.business.modules.param.dto.ReportDbParamQueryCriteria;
import com.bolt.report.business.modules.param.dto.converter.ReportDbParamConverter;
import com.bolt.report.business.modules.param.entity.ReportDbParamEntity;
import com.bolt.report.business.modules.param.service.ReportDbParamService;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* Auto Generate Javadoc
* @author
* 日期:2021-11-17 14:16:42
* 
**/
@RestController
@RequestMapping("report/design/reportDbParam")
@RequiredArgsConstructor
@Api(tags ="")
public class ReportDbParamController  {

private final ReportDbParamService reportDbParamService;

    /**
    * 列表分页
    * @return
    */
    @PostMapping(value = "/page")
    @ApiOperation(notes = "列表分页列表分页", httpMethod = "GET", value = "列表分页")
    public ResultMessage list(Pageable pageable,ReportDbParamQueryCriteria queryCriteria) {
        PageInfo <ReportDbParamEntity> pageInfo =  reportDbParamService.findPage(pageable,(root, criteriaQuery, criteriaBuilder)
            -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
            return Results.success(pageInfo.map(K -> ReportDbParamConverter.toDTO(K)));
    }

    /**
    * 根据ID操作
    */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询", httpMethod = "GET", value = "根据ID查询")
    public ResultMessage <ReportDbParamDTO> get(@PathVariable String id) {
        ReportDbParamEntity reportDbParamEntity = reportDbParamService.findOne(id).get();
        return Results.success(  ReportDbParamConverter.toDTO(reportDbParamEntity));
    }

    /**
    * 新增操作
    * @param dao 表单数据
    * @return
    */
    @PostMapping
    @ApiOperation(notes = "新增", httpMethod = "POST", value = "新增")
    public ResultMessage<Void> create(@Validated @RequestBody ReportDbParamDTO dao) {
      reportDbParamService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
    * 编辑操作
    * @param dao 表单数据
    * @return
    */
    @PutMapping
    @ApiOperation(notes = "编辑", httpMethod = "PUT", value = "编辑")
    public ResultMessage<Void> update(@Validated(ReportDbParamDTO.Update.class) @RequestBody ReportDbParamDTO dao) {
        reportDbParamService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
    * 删除操作
    */
     @DeleteMapping
     @ApiOperation(notes = "根据ID删除", httpMethod = "DELETE", value = "根据ID删除")
     public ResultMessage<Void> doDelete(@RequestBody Set<String> ids) {
         for (String id : ids) {
           reportDbParamService.deleteById(id);
         }
         return Results.success();
    }
 }